package com.hdphone.zljutils.impl;

import com.hdphone.zljutils.inter.IMmkvUtil;
import com.tencent.mmkv.MMKV;

import java.util.Set;

/**
 * @author sq
 * @date 2020/10/19
 * @describe
 */
public class MmkvUtilImpl implements IMmkvUtil {

    @Override
    public void setStringValue(String key, String value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getStringValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeString(key);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    @Override
    public String getStringValue(String key, String defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeString(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }


    @Override
    public void setLongValue(String key, long value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getLongValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeLong(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public long getLongValue(String key, long defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeLong(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    @Override
    public void setIntValue(String key, int value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getIntValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeInt(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getIntValue(String key, int defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeInt(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    @Override
    public void setDoubleValue(String key, double value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public double getDoubleValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeDouble(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0.0;
        }
    }

    @Override
    public double getDoubleValue(String key, double defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeDouble(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    @Override
    public void setFloatValue(String key, float value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public float getFloatValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeFloat(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0.0f;
        }
    }

    @Override
    public float getFloatValue(String key, float defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeFloat(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    @Override
    public void setBooleanValue(String key, boolean value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean getBooleanValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeBool(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean getBooleanValue(String key, boolean defValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeBool(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defValue;
        }
    }

    @Override
    public void setSetValue(String key, Set<String> value) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.encode(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Set<String> getSetValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeStringSet(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Set<String> getSetValue(String key, Set<String> defaultValue) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.decodeStringSet(key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    @Override
    public void clearAll() {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.clearAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeKeyForValue(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            kv.removeValueForKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean containsKey(String key) {
        try {
            MMKV kv = MMKV.defaultMMKV();
            return kv.containsKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
